package com.lkd.controller;

import cn.hutool.core.util.ObjectUtil;
import com.lkd.entity.User;
import com.lkd.exception.BusinessException;
import com.lkd.service.IUserService;
import com.lkd.vo.LoginUserVo;
import com.lkd.vo.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user-service/user")
@RequiredArgsConstructor
@Slf4j

public class LoginController {

    private final IUserService userService;


    //伪登录
//    public Result login(){
//        LoginUserVo userVo = new LoginUserVo();
//        userVo.setToken("123");
//        return Result.success(userVo);
//    }


    @PostMapping("/login")
    public Result login(@RequestBody User user) {//前端传递的数据是JSON格式，所以要用@RequestBody
        log.info("用户登录:{}", user);          //用户名和密码都封装到user对象中

        //调用service，封装，返回结果
        LoginUserVo loginUserVo = userService.login(user.getLoginName(), user.getPassword());
        if (ObjectUtil.isNull(loginUserVo)) {
            log.info("登录失败~");
            throw new BusinessException("登录失败");
        } else {
            return Result.success(loginUserVo);
        }
    }
}
